package com.fenghongzhang.day008view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyView2 extends LinearLayout {


    private static final String TAG = "MyView2";
    PointF pointF = new PointF();

    LinearLayout.LayoutParams layoutParams;
    LinearLayout linearLayout;
    public MyView2(@NonNull Context context) {
        super(context);
        init();
    }

    public MyView2(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
         linearLayout = new LinearLayout(getContext());
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200);
        layoutParams.topMargin = -200;
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.setBackgroundColor(Color.RED);

        addView(linearLayout);
    }


    private void statrAnim(int start,int end){
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start,end);
        valueAnimator.setDuration(500);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer animatedValue = (Integer) animation.getAnimatedValue();
                 layoutParams = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
                layoutParams.topMargin = animatedValue;
                linearLayout.setLayoutParams(layoutParams);
            }
        });
        valueAnimator.start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){

            case MotionEvent.ACTION_DOWN:
                pointF.y = event.getY();
                pointF.x = event.getX();
                break;
            case MotionEvent.ACTION_UP:

                layoutParams = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
                if(layoutParams.topMargin < -200 / 2){
                    statrAnim(layoutParams.topMargin,-200);
                }else{
                    statrAnim(layoutParams.topMargin,0);
                }

                break;
            case MotionEvent.ACTION_MOVE:

                layoutParams = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
                layoutParams.topMargin  += event.getY() - pointF.y;

                if(layoutParams.topMargin > 0){
                    layoutParams.topMargin = 0;
                }
                if(layoutParams.topMargin < -200){
                    layoutParams.topMargin = -200;
                }

                linearLayout.setLayoutParams(layoutParams);

                pointF.x = event.getX();
                pointF.y = event.getY();
                break;
        }

        return true;

    }
}
